package ui;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;
import java.awt.FlowLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;

public class ProgramChairPanel extends JInternalFrame {
  
  private static final String[] TABLE_HEADERS = {
    "Author",
    "Manuscript",
    "R1",
    "R2",
    "R3",
    "R4",
    "SPC",
    "Decision"
  };
  
  private static final Object[][] TABLE_DATA = {
    {"Patricia Diaz", "How to make an omelette", 3, 4, 0, 0, 0, "Undecided"},
    {"Aaron Chen", "Diaries of a hobbyist gamemaker", 4, 3, 4, null, null, "Undecided"},
    {"Romero Anderson", "Speedrunning for beginners", 3, null, null, null, null, "Undecided"},
    {"Emily Rydik", "The Silver Bullet", null, null, 5, 5, null, "Undecided"},
    {"Won Choi", "Quality soup dumplings", 3, 4, 4, 2, null, "Undecided"}
  };
  
  private static final boolean[] TABLE_BUTTON_COLUMNS = {
    false, //Author
    true, //Manuscript
    false, //R1
    false, //R2
    false, //R3
    false, //R4
    false, //SPC
    true   //Decision
  };
  
  private static final int[] TABLE_PREFERRED_WIDTH = {
    110, 250, 30, 30, 30, 30, 40, 70
  };
  
  private JTable table;
  
  
  /**
   * Create the internal frame, which acts as a panel.
   */
  public ProgramChairPanel() {
    setFrameParameters();
    final JPanel main_panel = createMainPanel();
    getContentPane().add(main_panel);
    
    pack();
    setResizable(false);
    setVisible(true);
  }

  /**
   * Sets up the internal frame's properties.
   */
  protected void setFrameParameters() {
    setBackground(Standards.COLOR_BACKGROUND);
    setBorder(null);
    final javax.swing.plaf.InternalFrameUI ifu = getUI();
    ((javax.swing.plaf.basic.BasicInternalFrameUI) ifu).setNorthPane(null);  
    setBorder(null);
    getContentPane().setLayout(new FlowLayout());
    setFocusable(true);
  }
  
  private void setUpDecisionColumn() {
    JComboBox<String> box = new JComboBox<String>();
    box.addItem("Undecided");
    box.addItem("Accepted");
    box.addItem("Rejected");
    TableColumn column = table.getColumnModel().getColumn(TABLE_HEADERS.length - 1);
    column.setCellEditor(new DefaultCellEditor(box));
  }
  
  /**
   * Create the frame.
   */
  private JPanel createMainPanel() {
    final JPanel panel_main = new JPanel();
    
    table = new CustomTable(TABLE_DATA, TABLE_HEADERS, TABLE_BUTTON_COLUMNS);
    
    final TableColumnModel column_model = table.getColumnModel();
    final int n = Math.min(column_model.getColumnCount(), TABLE_PREFERRED_WIDTH.length);
    for (int i = 0; i < n; i++) {
      column_model.getColumn(i).setPreferredWidth(TABLE_PREFERRED_WIDTH[i]);
    }
    
    table.setBackground(Standards.COLOR_TABLE_ODD_ROW);
    ((CustomTable) table).setAlternateBackground(Standards.COLOR_TABLE_EVEN_ROW);
    table.setSelectionBackground(Color.YELLOW);
    //table.setValueAt(COMBO, 3, 7);
    
    panel_main.add(table.getTableHeader());
    panel_main.setLayout(new BoxLayout(panel_main, BoxLayout.Y_AXIS));
    panel_main.add(table);

    return panel_main;
  }

}
